package com.mszlu.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.dao.mapper.SysUserMapper;
import com.mszlu.news.dao.pojo.SysUser;
import com.mszlu.news.service.LoginService;
import com.mszlu.news.service.SysUserService;
import com.mszlu.news.vo.ErrorCode;
import com.mszlu.news.vo.LoginUserVo;
import com.mszlu.news.vo.Result;
import com.mszlu.news.vo.UserVo;
import com.mszlu.news.vo.params.PageParams;
import com.mszlu.news.vo.params.SysUserParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private LoginService loginService;

    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("jack");
        }
        UserVo userVo  = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);//将sysUser的javabean赋给userVo
        userVo.setId(String.valueOf(sysUser.getId()));//将sysUser的id取出，UserVo的id以string存储
        return userVo;
    }

    @Override
    public SysUser findUserById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setNickname("jack");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
        queryWrapper.last("limit 1");

        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Result findUserByToken(String token) {
        /**
         * 1. token合法性校验
         *    是否为空，解析是否成功 redis是否存在
         * 2. 如果校验失败 返回错误
         * 3. 如果成功，返回对应的结果 LoginUserVo
         */
        SysUser sysUser = loginService.checkToken(token);
        if (sysUser == null){
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(),ErrorCode.TOKEN_ERROR.getMsg());
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setNickname(sysUser.getNickname());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setAccount(sysUser.getAccount());
        return Result.success(loginUserVo);//将赋完值的loginUserVo放在Result的success方法返回
    }

    @Override
    public Result findUserInfoById(Long id){
        SysUser sysUser = sysUserMapper.selectById(id);
        UserVo userVo  = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        userVo.setId(String.valueOf(sysUser.getId()));
        return Result.success(userVo);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        return this.sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        //保存用户这 id会自动生成
        //这个地方 默认生成的id是 分布式id 雪花算法
        //mybatis-plus
        this.sysUserMapper.insert(sysUser);
    }

    @Override
    public Result findAllUser(PageParams params) {
        LambdaQueryWrapper<SysUser> userQueryWrapper=new LambdaQueryWrapper<>();
        userQueryWrapper.orderByDesc(SysUser::getV);
        Page<SysUser> page=new Page<>(params.getPage(),params.getPageSize());
        IPage<SysUser> sysUserIPage=sysUserMapper.selectPage(page,userQueryWrapper);
        return Result.success(sysUserIPage);
    }

    private static final String slat = "mszlu!@#";

    @Override
    public Result updateUser(SysUserParam sysUserParam) {
        SysUser sysUser=new SysUser();
        sysUser.setId(sysUserParam.getId());
        sysUser.setAge(sysUserParam.getAge());
        sysUser.setArea(sysUserParam.getArea());
        sysUser.setAvatar(sysUserParam.getAvatar());
        sysUser.setDesign(sysUserParam.getDesign());
        sysUser.setEmail(sysUserParam.getEmail());
        sysUser.setHobby(sysUserParam.getHobby());
        sysUser.setMobilePhoneNumber(sysUserParam.getMobilePhoneNumber());
        sysUser.setNickname(sysUserParam.getNickname());
        sysUser.setPassword(DigestUtils.md5Hex(sysUserParam.getPassword()+slat));
        sysUser.setWork(sysUserParam.getWork());
        sysUser.setSex(sysUserParam.getSex());
        sysUserMapper.updateById(sysUser);
        return Result.success("更新用户成功");
    }

    @Override
    public Result selectUser(SysUserParam sysUserParam) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("nickname",sysUserParam.getNickname());
        List<SysUser> sysUserList=sysUserMapper.selectList(queryWrapper);
        return Result.success(sysUserList);
    }

}
